package l.l.w.leetcode.RemoveElement;

/**
 * Created by llw on 2017/11/3.
 */
public class RemoveElement {
    public int removeElement(int[] nums, int val) {
        int result = 0;
        for(int i = 0 ; i < nums.length; ++i){
            if(nums[i] == val){
                nums[i] = Integer.MIN_VALUE;
            }else{
                result++;
            }
        }
        int j = 0;
        for(int i = 0 ; i < nums.length;++i){
            if(nums[i] != Integer.MIN_VALUE){
                exchange(nums,i,j);
                j++;
            }
        }
        return result;
    }

    private void exchange(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
